﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Data.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Time.Model;

namespace Time.App
{
    public partial class SignInWindow : Form
    {
        private TimeDataContext db = new TimeDataContext();
        public SignInWindow(Form owner)
        {
            this.Owner = owner;
            InitializeComponent();
        }

        private void btnSignIn_Click(object sender, EventArgs e)
        {
            string emailAddress = txtEmailAddress.Text.ToLower();
            string password = txtPassword.Text;

            ISingleResult<LogInResult> res = db.LogIn(emailAddress, password, Time.Model.Constants.PASSWORD_SALT);
            if (res.Count() == 1)
            {
                Program.User = new User { FirstName = "asd" };
                object resObj = res.ToList()[0];
                MessageBox.Show(this, "Welcome, " + Program.User.FirstName + "!", "Welcome", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show(this, "Wrong e-mail address or password!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtPassword.Text = "";
            }
        }
    }
}
